package redis.clients.util;

import redis.clients.jedis.Tuple;

import java.util.List;
import java.util.Set;

public class ObjectSize {

    public static void main(String[] args) {
        final String v = "你好";
        System.out.println(sizeOf(v));
        System.out.println(v.getBytes().length);
        //System.out.println(sizeOf(1));
        //System.out.println(sizeOf(1L));
        //System.out.println(sizeOf(new int[]{1, 2, 3}));
    }

    public static long sizeOf(Object res) {
        //return RamUsageEstimator.sizeOf(res);

        long responseSize = 0;
        if (res == null) {
            responseSize = 0;

        } else if (res instanceof Set) {
            for (Object o : ((Set) res)) {
                responseSize += sizeOf(o);
            }
        } else if (res instanceof List) {
            for (Object o : ((List) res)) {
                responseSize += sizeOf(o);
            }
        } else if (res instanceof String) {
            responseSize = ((String) res).length() * 2;

        } else if (res instanceof Byte) {
            responseSize = 1;

        } else if (res instanceof Boolean) {
            responseSize = 1;

        } else if (res instanceof Character) {
            responseSize = 2;

        } else if (res instanceof Short) {
            responseSize = 2;

        } else if (res instanceof Integer) {
            responseSize = 4;

        } else if (res instanceof Float) {
            responseSize = 4;

        } else if (res instanceof Long) {
            responseSize = 8;

        } else if (res instanceof Double) {
            responseSize = 8;

        } else if (res instanceof Byte[]) {
            responseSize = ((Byte[]) res).length;

        } else if (res instanceof byte[]) {
            responseSize = ((byte[]) res).length;

        } else if (res instanceof Boolean[]) {
            responseSize = ((Boolean[]) res).length;

        } else if (res instanceof boolean[]) {
            responseSize = ((boolean[]) res).length;

        } else if (res instanceof Character[]) {
            responseSize = 2 * ((Character[]) res).length;

        } else if (res instanceof char[]) {
            responseSize = 2 * ((char[]) res).length;

        } else if (res instanceof Short[]) {
            responseSize = 2 * ((Short[]) res).length;

        } else if (res instanceof short[]) {
            responseSize = 2 * ((short[]) res).length;

        } else if (res instanceof Integer[]) {
            responseSize = 4 * ((Object[]) res).length;

        } else if (res instanceof int[]) {
            responseSize = 4 * ((int[]) res).length;

        } else if (res instanceof Float[]) {
            responseSize = 4 * ((Float[]) res).length;

        } else if (res instanceof float[]) {
            responseSize = 4 * ((float[]) res).length;

        } else if (res instanceof Long[]) {
            responseSize = 8 * ((Long[]) res).length;

        } else if (res instanceof long[]) {
            responseSize = 8 * ((long[]) res).length;

        } else if (res instanceof Double[]) {
            responseSize = 8 * ((Double[]) res).length;

        } else if (res instanceof double[]) {
            responseSize = 8 * ((double[]) res).length;

        } else if (res instanceof Tuple) {
            responseSize = ((Tuple) res).getBinaryElement().length + 8;

        } else {
            throw new RuntimeException("UnsupportedDataTypeException " + res.getClass().getName());
        }

        return responseSize;
    }

}
